# Copyright (C) 2006, 2007  Stephane Alnet
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# 

#
# For more information visit http://carrierclass.net/
#

package CCNCore::Model::StaticDataTypes;
use strict; use warnings;

=pod

    my $registry = new CCN::Framework::Model::DataType::Registry;
    CCN::Content::Type::populate($registry);

=cut

sub populate($)
{
    my $registry = shift;

    # Taken from CCN::Context.pm
    my $qr_ip       = qr(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3});
    my $qr_name     = qr([a-zA-Z]\w{0,14});
    my $qr_dns      = qr([a-zA-Z][\w.-]+\.[\w.-]{1,126});

    $registry->register(

        # These should go into the framework
        'name' => {
            'validate'  => sub { return $_[1] =~ /^\s*${qr_name}\s*$/i; },
            'cleanup'   => sub { lc(trimo($_[1])) },
            'html'      => undef, # no transformation
            'input'     => sub { $_[0]->cgi->textfield(-name=>$_[1],-size=>16,-maxlength=>15,-class=>'validate',-default=>$_[2],-force=>(defined$_[2])) },
        },

        'label' => {
            'validate'  => sub { length($_[1]) > 0 and length($_[1]) < 128; },
            'input'     => sub { $_[0]->cgi->textfield(-name=>$_[1],-size=>16,-maxlength=>127,-class=>'validate',-default=>$_[2],-force=>(defined$_[2])) },
        },
        
        'pk' => {
            'validate'  => sub { $_[1] =~ /^\d+$/ },
            'input'     => sub { '' },
        },

        'login' => {
            'validate'  => sub { $_[1] =~ /^[\w.\/-]{1,64}$/; },
            'cleanup'   => sub { lc(trimo($_[1])) },
        },

        'password' => {
            'validate'  => sub { length($_[1]) > 0 and length($_[1]) < 255; },
            'input'     => sub { $_[0]->cgi->textfield(-name=>$_[1],-size=>16,-maxlength=>127,-class=>'validate',-value=>$_[2],-force=>(defined$_[2])) },
        },

        'ui_item' => {
            'validate'  => sub { length($_[1]) < 1024 and $_[1] =~ /^(\w+:)?\w+(_\w+)*$/ },
        },

        'string' => {
            'validate'  => sub { length($_[1]) < 1024; }
        },

        'longstring' => {
            'validate'  => sub { length($_[1]) < 1024; },
            'input'     => sub { $_[0]->cgi->textarea(-name=>$_[1],-maxlength=>1023,-class=>'validate',-default=>$_[2],-force=>(defined$_[2])) },
        },

        'language' => {
            'validate'  => sub { length($_[1]) == 2; },
            'input'     => sub { $_[0]->cgi->textfield(-name=>$_[1],-size=>2,-maxlength=>3,-class=>'validate',-default=>$_[2],-force=>(defined$_[2])) },
        },

        'digits' => {
            'validate'  => sub { $_[1] =~ /^\d{1,9}$/ },
            'input'     => sub { $_[0]->cgi->textfield(-name=>$_[1],-size=>10,-maxlength=>9,-class=>'validate',-default=>$_[2],-force=>(defined$_[2])) },
        },

        'float' => {
            # 'float' are stored as REAL, which works for both SQLite3 and MySQL (and presumably PostgreSQL)
            'validate'  => sub { $_[1] =~ /^[*#\d.]{1,16}$/ },
            'input'     => sub { $_[0]->cgi->textfield(-name=>$_[1],-size=>17,-maxlength=>16,-class=>'validate',-default=>$_[2],-force=>(defined$_[2])) },
        },
        'dns' => {
            'validate'  => sub { return $_[1] =~ /^${qr_dns}$/i; },
            'cleanup'   => sub { lc(trimo($_[1])) },
            'input'     => sub { $_[0]->cgi->textfield(-name=>$_[1],-size=>17,-maxlength=>126,-class=>'validate',-default=>$_[2],-force=>(defined$_[2])) },
        },
        'ip' => {
            'validate'  => sub { $_[1] =~ /^\s*${qr_ip}\s*$/ },
            'cleanup'   => sub { trimo($_[1]) },
            'input'     => sub { $_[0]->cgi->textfield(-name=>$_[1],-size=>17,-maxlength=>16,-class=>'validate',-default=>$_[2],-force=>(defined$_[2])) },
        },
        'mac' => {
            'validate'  => sub { $_[1] =~ /^\s*[\da-f:.]+\s*$/i },
            'cleanup'   => sub { CCN::Text::qr_mac_clean($_[1]) },
            'input'     => sub { $_[0]->cgi->textfield(-name=>$_[1],-size=>18,-maxlength=>17,-class=>'validate',-default=>$_[2],-force=>(defined$_[2])) },
        },
        'timezone' => {
        },
        'boolean' => {
            'validate'  => sub { $_[1] =~ /^(TRUE|FALSE|NULL|)$/ },
            'cleanup'   => sub { return $_[1] if $_[1] eq 'TRUE' or $_[1] eq 'FALSE'; return undef; },
            'input' => sub { 
                $_[0]->cgi->popup_menu(-name=>$_[1],-class=>'validate',-default=>$_[2],-force=>(defined$_[2]), 
                    -values=>['','TRUE','FALSE'], -labels=>{''=>'','TRUE'=>'Yes', 'FALSE'=>'No'}); 
            },
        },
        'country' => {
        },


    );
}

1;
